<template>
  <div class="app-container">

    <!-- 搜索工作栏 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="订单号" prop="saleOrderNo">
        <el-input v-model="queryParams.saleOrderNo" placeholder="请输入订单号" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="商户单订单号" prop="merchantOrderNo">
        <el-input v-model="queryParams.merchantOrderNo" placeholder="请输入商户单订单号" clearable
                  @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item label="订单状态" prop="saleStatus">
        <el-select v-model="queryParams.saleStatus" placeholder="请选择订单状态" clearable size="small">
          <el-option v-for="dict in statusDictDatas"
                     :key="dict.value" :label="dict.label" :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="销售人员" prop="saleUserName">
        <el-input v-model="queryParams.saleUserName" placeholder="请输入销售人员" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入联系方式" clearable @keyup.enter.native="handleQuery"/>
      </el-form-item>

      <el-form-item label="订单时间" prop="createTime">
        <el-date-picker v-model="queryParams.createTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>


      <el-form-item label="完成时间" prop="finishTime">
        <el-date-picker v-model="queryParams.finishTime" style="width: 240px" value-format="yyyy-MM-dd HH:mm:ss"
                        type="daterange"
                        range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"/>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 操作工具栏 -->
    <el-row :gutter="10" class="mb8">
      <!--      <el-col :span="1.5">
              <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                         v-hasPermi="['pekingfish:sale-order:create']">新增</el-button>
            </el-col>-->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
                   :loading="exportLoading"
                   v-hasPermi="['pekingfish:sale-order:export']">导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" border  :max-height="tableHeight" :data="list">
      <el-table-column label="操作" align="center" fixed="left" class-name="small-padding fixed-width">
        <template v-slot="scope">
          <el-button size="mini" type="text" @click="handleView(scope.row)"
                     v-hasPermi="['pekingfish:sale-order:detail']">详情
          </el-button>
          <el-button size="mini" type="text" @click="inbound(scope.row)"
                     v-if="scope.row.inboundStatus == 0 && scope.row.saleStatus == 3  "
                     v-hasPermi="['pekingfish:sale-order:inbound']">入库
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="订单号" align="center" prop="saleOrderNo" width="150px"/>
      <el-table-column label="商户单订单号" align="center" prop="merchantOrderNo" width="150px" fixed="left"/>
      <el-table-column label="订单状态" align="center" prop="saleStatus">
        <template v-slot="scope">
          <dict-tag :type="DICT_TYPE.sale_order_status" :value="scope.row.saleStatus"/>
        </template>
      </el-table-column>

      <el-table-column label="手机品牌" align="center" prop="brand"/>
      <el-table-column label="手机型号" align="center" prop="model"/>
      <el-table-column label="型号类别" align="center" prop="modelType"/>
      <el-table-column label="回收价" align="center" prop="paybackPrice"/>
      <el-table-column label="销售回收价" align="center" prop="salePaybackPrice"/>
      <el-table-column label="扣款金额" align="center" prop="deductionPrice"/>
      <el-table-column label="销售人员" align="center" prop="saleUserName"/>
      <el-table-column label="联系方式" align="center" prop="phone"/>
      <el-table-column label="订单时间" align="center" prop="createTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="完成时间" align="center" prop="finishTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.finishTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收货时间" align="center" prop="harvestTime" width="180">
        <template v-slot="scope">
          <span>{{ parseTime(scope.row.harvestTime) }}</span>
        </template>
      </el-table-column>

    </el-table>
    <!-- 分页组件 -->
    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNo" :limit.sync="queryParams.pageSize"
                @pagination="getList"/>

    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="title" :visible.sync="open" width="500px" v-dialogDrag append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="销售预订单id" prop="saleBiddingId">
          <el-input v-model="form.saleBiddingId" placeholder="请输入销售预订单id"/>
        </el-form-item>
        <el-form-item label="商户单订单号" prop="merchantOrderNo">
          <el-input v-model="form.merchantOrderNo" placeholder="请输入商户单订单号"/>
        </el-form-item>
        <el-form-item label="手机品牌" prop="brand">
          <el-input v-model="form.brand" placeholder="请输入手机品牌"/>
        </el-form-item>
        <el-form-item label="手机品牌" prop="brandId">
          <el-input v-model="form.brandId" placeholder="请输入手机品牌"/>
        </el-form-item>
        <el-form-item label="手机型号" prop="model">
          <el-input v-model="form.model" placeholder="请输入手机型号"/>
        </el-form-item>
        <el-form-item label="手机型号" prop="modelId">
          <el-input v-model="form.modelId" placeholder="请输入手机型号"/>
        </el-form-item>
        <el-form-item label="型号类别" prop="modelType">
          <el-select v-model="form.modelType" placeholder="请选择型号类别">
            <el-option label="请选择字典生成" value=""/>
          </el-select>
        </el-form-item>
        <el-form-item label="型号类型" prop="modelTypeId">
          <el-input v-model="form.modelTypeId" placeholder="请输入型号类型"/>
        </el-form-item>
        <el-form-item label="回收价" prop="paybackPrice">
          <el-input v-model="form.paybackPrice" placeholder="请输入回收价"/>
        </el-form-item>
        <el-form-item label="销售回收价" prop="salePaybackPrice">
          <el-input v-model="form.salePaybackPrice" placeholder="请输入销售回收价"/>
        </el-form-item>
        <el-form-item label="订单状态" prop="saleStatus">
          <el-radio-group v-model="form.saleStatus">
            <el-radio label="1">请选择字典生成</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="扣款金额" prop="deductionPrice">
          <el-input v-model="form.deductionPrice" placeholder="请输入扣款金额"/>
        </el-form-item>
        <el-form-item label="销售人员Id" prop="saleUserId">
          <el-input v-model="form.saleUserId" placeholder="请输入销售人员Id"/>
        </el-form-item>
        <el-form-item label="联系方式" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入联系方式"/>
        </el-form-item>
        <el-form-item label="完成时间" prop="finishTime">
          <el-date-picker clearable v-model="form.finishTime" type="date" value-format="timestamp"
                          placeholder="选择完成时间"/>
        </el-form-item>
        <el-form-item label="收货时间" prop="harvestTime">
          <el-date-picker clearable v-model="form.harvestTime" type="date" value-format="timestamp"
                          placeholder="选择收货时间"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>


    <!-- 订单详情 -->
    <el-dialog title="订单详情" :visible.sync="openDetail" width="800px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px" size="mini">
        <el-row>
          <el-col :span="30">
            <el-form-item label="品牌：">{{ form.brand }}</el-form-item>
          </el-col>
          <el-col :span="30">
            <el-form-item label="手机型号：">{{ form.model }}</el-form-item>
          </el-col>
          <el-col :span="30">
            <el-form-item label="型号类型：">{{ form.modelType }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="IMEI：">{{ form.imei }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="参数：">{{ form.memory }}，{{ form.colour }}，{{ form.buy }}，{{
                form.network
              }}
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="机况：">{{ form.subrackDesc }}</el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="图片：">
              <imageUpload v-model="form.reportImg"/>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="openDetail = false">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  saleOrderInboud,
  createSaleOrder,
  updateSaleOrder,
  deleteSaleOrder,
  getSaleOrder,
  getSaleOrderPage,
  exportSaleOrderExcel
} from "@/api/pekingfish/saleOrder";
import {DICT_TYPE, getDictDatas} from "@/utils/dict";
import ImageUpload from '@/components/ImageUpload'
import ImagePreview from '@/components/ImagePreview'
import VideoUpload from "@/components/VideoUpload";
import Editor from "@/components/Editor";

export default {
  name: "SaleOrder",
  components: {
    VideoUpload, Editor,
    ImageUpload, ImagePreview,
  },
  data() {
    return {
      statusDictDatas: getDictDatas(DICT_TYPE.sale_order_status),
      tableHeight: document.documentElement.scrollHeight - 245 + "px",
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 销售单列表
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        saleOrderNo: null,
        pageNo: 1,
        pageSize: 10,
        saleBiddingId: null,
        merchantOrderNo: null,
        createTime: [],
        brand: null,
        brandId: null,
        model: null,
        modelId: null,
        modelType: null,
        modelTypeId: null,
        paybackPrice: null,
        salePaybackPrice: null,
        saleStatus: null,
        deductionPrice: null,
        saleUserId: null,
        phone: null,
        finishTime: [],
        harvestTime: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        merchantOrderNo: [{required: true, message: "商户单订单号不能为空", trigger: "blur"}],
      },
      openDetail: false,

    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询列表 */
    getList() {
      this.loading = true;
      // 执行查询
      getSaleOrderPage(this.queryParams).then(response => {
        this.list = response.data.list;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    /** 取消按钮 */
    cancel() {
      this.open = false;
      this.reset();
    },
    /** 表单重置 */
    reset() {
      this.form = {
        id: undefined,
        saleBiddingId: undefined,
        merchantOrderNo: undefined,
        brand: undefined,
        brandId: undefined,
        model: undefined,
        modelId: undefined,
        modelType: undefined,
        modelTypeId: undefined,
        paybackPrice: undefined,
        salePaybackPrice: undefined,
        saleStatus: undefined,
        deductionPrice: undefined,
        saleUserId: undefined,
        phone: undefined,
        finishTime: undefined,
        harvestTime: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNo = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加销售单";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id;
      getSaleOrder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改销售单";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (!valid) {
          return;
        }
        // 修改的提交
        if (this.form.id != null) {
          updateSaleOrder(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.open = false;
            this.getList();
          });
          return;
        }
        // 添加的提交
        createSaleOrder(this.form).then(response => {
          this.$modal.msgSuccess("新增成功");
          this.open = false;
          this.getList();
        });
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id;
      this.$modal.confirm('是否确认删除销售单编号为"' + id + '"的数据项?').then(function () {
        return deleteSaleOrder(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      // 处理查询参数
      let params = {...this.queryParams};
      params.pageNo = undefined;
      params.pageSize = undefined;
      this.$modal.confirm('是否确认导出所有销售单数据项?').then(() => {
        this.exportLoading = true;
        return exportSaleOrderExcel(params);
      }).then(response => {
        this.$download.excel(response, '销售单.xls');
        this.exportLoading = false;
      }).catch(() => {
      });
    },
    /** 详细按钮操作 */
    handleView(row) {
      this.openDetail = true
      this.form = row
    },

    /** 删除按钮操作 */
    inbound(row) {
      const id = row.id;
      this.$modal.confirm('是否确认入库?').then(function () {
        return saleOrderInboud(id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("入库成功");
      }).catch(() => {
      });
    },
  }
};
</script>
